/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.Enumeration;

public abstract class SettingsStore {
	abstract protected void set(String name, String value);
	abstract protected String get(String name);
	abstract public void save();
	abstract public Enumeration getSettingNames();

	public abstract boolean doesNameExist(String name);
	
	public String getString(String name, String default_value) {
		String value = get(name);
		if (value == null) {
			value = default_value;
			set(name,default_value);
			save();
		}
		return value;
	}

	public long getLong(String name, long default_value) {
		String value = getString(name, ""+default_value);
		long lvalue = default_value;
		try {
			lvalue = Long.parseLong(value);
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		return lvalue;
	}

	public int getInt(String name, int default_value) {
		String value = getString(name, ""+default_value);
		int ivalue = default_value;
		try {
			ivalue = Integer.parseInt(value);
		} catch (NumberFormatException nfe) {
			Log.debug.out( nfe);
		}
		return ivalue;
	}

	public boolean getBoolean(String name, boolean default_value) {
		return Boolean.valueOf(getString(name, "" + default_value)).booleanValue();
	}

	public Date getTime(String name, Date default_value) {
		String value = null;
		if (default_value != null) {
			value = getString(name, Settings.time_format.format(default_value));
		} else {
			value = getString(name, null);
		}
		Date tvalue = default_value;
		try {
			tvalue = Settings.time_format.parse(value);
		} catch (ParseException pe) {
			Log.debug.out( pe);
		}
		return tvalue;
	}

	public Date getDate(String name, Date default_value) {
		String value = null;
		if (default_value != null) {
			value = getString(name, Settings.date_format.format(default_value));
		} else {
			value = getString(name, null);
		}
		Date tvalue = default_value;
		try {
			tvalue = Settings.date_format.parse(value);
		} catch (ParseException pe) {
			Log.debug.out( pe);
		}
		return tvalue;
	}


	public Date getDateTime(String name, Date default_value) {
		String value = null;
		if (default_value != null) {
			value = getString(name, Settings.date_time_format.format(default_value));
		} else {
			value = getString(name, null);
		}
		Date tvalue = default_value;
		try {
			tvalue = Settings.date_time_format.parse(value);
		} catch (ParseException pe) {
			Log.debug.out( pe);
		}
		return tvalue;
	}

	public void setString(String name, String value) {
		set(name, value);
	}

	public void setLong(String name, long value) {
		set(name, "" + value);
	}

	public void setInt(String name, int value) {
		set(name, "" + value);
	}

	public void setBoolean(String name, boolean value) {
		set(name, "" + value);
	}

	public void setTime(String name, Date value) {
		set(name, Settings.time_format.format(value));
	}

	public void setDate(String name, Date value) {
		set(name, Settings.date_format.format(value));
	}

	public void setDateTime(String name, Date value) {
		set(name, Settings.date_time_format.format(value));
	}
}

